<%= form_for @changeset, @action, [class: "ui form"], fn f -> %>
  <div class="two fields">
    <div class="two wide field required <%= AdminHelpers.error_class(f, :slug) %>">
      <%= label(f, :slug) %>
      <%= text_input(f, :slug, placeholder: "183", readonly: f.data.published) %>
      <%= AdminHelpers.error_message(f, :slug) %>
    </div>

    <div class="fourteen wide field">
      <%= label(f, :teaser) %>
      <%= text_input(f, :teaser, placeholder: "Cool, Stuff, You, Want, To, Read") %>
      <%= AdminHelpers.error_message(f, :teaser) %>
    </div>
  </div>

  <div class="field <%= AdminHelpers.error_class(f, :note) %>">
    <%= label(f, :note) %>
    <%= textarea(f, :note, placeholder: "Markdown enabled.", rows: 3) %>
    <%= AdminHelpers.error_message(f, :note) %>
  </div>

<%= if NewsIssue.is_published(@changeset.data) do %>
  <div class="field <%= AdminHelpers.error_class(f, :published_at) %>">
    <%= label(f, :published_at, "Publish Time") %>
    <%= AdminHelpers.semantic_calendar_field(f, :published_at) %>
    <%= AdminHelpers.error_message(f, :published_at) %>
  </div>
<% end %>

  <div class="field required">
    <%= label(f, :items) %>

    <div class="ui very relaxed large ordered divided list js-items">
    <%= inputs_for f, :news_issue_items, fn i -> %>
      <% item = AdminHelpers.load_from_form(i, NewsItem, :item) %>
      <div class="item <%= if AdminHelpers.is_persisted(i.data), do: "persisted" %>" style="background: white;">
        <%= hidden_input(i, :item_id) %>
        <%= hidden_input(i, :position, class: "js-position") %>

        <div class="right floated content">
        <%= if item.image do %>
          <div class="ui tiny basic icon button js-image" title="Toggle image visibility">
            <i class="eye icon <%= if !i.data.image, do: "slash" %>"></i>
            <%= hidden_input(i, :image) %>
          </div>
        <% end %>
          <div class="ui tiny basic icon button js-move"><i class="move icon"></i></div>
          <div class="ui tiny basic icon button js-remove"><i class="trash icon"></i></div>
          <%= hidden_input(i, :delete) %>
        </div>
        <div class="content" style="margin-left: 6px;">
          <%= link("#{item.headline} (#{SharedHelpers.ctr(item)}%)", to: item.url, class: "header") %>
          <div class="description" style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">
            <%= icon(item) %>
            <%= item.story |> SharedHelpers.md_to_text() %>
          </div>
        </div>
      </div>
    <% end %>
    </div>
  </div>

  <div class="ui divider"></div>

  <div class="field">
    <%= label(f, :ads) %>

    <div class="ui very relaxed large ordered divided list js-ads">
    <%= inputs_for f, :news_issue_ads, fn i -> %>
      <% ad = AdminHelpers.load_from_form(i, NewsAd, :ad) %>
      <div class="item <%= if AdminHelpers.is_persisted(i.data), do: "persisted" %>" style="background: white;">
        <%= hidden_input(i, :ad_id) %>
        <%= hidden_input(i, :position, class: "js-position") %>

        <div class="right floated content">
        <%= if ad.image do %>
          <div class="ui tiny basic icon button js-image" title="Toggle image visibility">
            <i class="eye icon <%= if !i.data.image, do: "slash" %>"></i>
            <%= hidden_input(i, :image) %>
          </div>
        <% end %>
          <div class="ui tiny basic icon button js-move"><i class="move icon"></i></div>
        </div>
        <div class="content" style="margin-left: 6px;">
          <%= link("#{ad.headline} (#{SharedHelpers.ctr(ad)}%)", to: ad.url, class: "header") %>
          <div class="description" style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">
            <%= icon(ad) %>
            <%= ad.story |> SharedHelpers.md_to_text() %>
          </div>
        </div>
      </div>
    <% end %>
    </div>
  </div>

  <div class="ui hidden divider"></div>

  <div class="ui equal width stackable grid">
  <%= if !AdminHelpers.is_persisted(f.data) do %>
    <div class="column"><%= AdminHelpers.submit_button(:primary, "Create") %></div>
    <div class="column"><%= AdminHelpers.submit_button(:secondary, "Create and Close", AdminHelpers.next_param(@conn, ~p"/admin/news/issues")) %></div>
  <% else %>
    <div class="column"><%= AdminHelpers.submit_button(:primary, "Save", SharedHelpers.current_path(@conn)) %></div>
    <div class="column"><%= AdminHelpers.submit_button(:secondary, "Save and Close", AdminHelpers.next_param(@conn, ~p"/admin/news/issues")) %></div>
  <% end %>
  <% end %>
  <div class="column">
  <%= cond do %>
  <% NewsIssue.is_published(@changeset.data) -> %>
    <%= link "Unpublish", to: "#{@action}/unpublish", class: "ui red fluid basic button", method: :post, data: [confirm: "Are you sure?"] %>
  <% NewsIssue.is_publishable(@changeset.data) -> %>
    <%= link "Publish", to: "#{@action}/publish", class: "ui green fluid basic button", method: :post %>
  <% true -> %>
    <button class="ui green fluid basic disabled button">Publish</button>
  <% end %>
  </div>
</div>
